package com.youfang.sixthmedia;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.gzit.view.FlipIndicatorView;
import com.youfang.sixthmedia.common.YoufangContext;

public class ManualActivity extends Activity implements OnGestureListener {

	private ViewFlipper flipper;

	private GestureDetector detector;

	private ManualActivity that = this;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.manual_activity);
		detector = new GestureDetector(this, this);
		flipper = (ViewFlipper) this.findViewById(R.id.manual_image_container);

	}

	private void gotoMainActivity() {

		Intent intent = new Intent();
		intent.setClass(this, MainActivity.class);
		this.startActivity(intent);
		this.finish();

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		this.detector.onTouchEvent(event);
		return true;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		boolean fling = false;// 滑动事件是否已经被接受
		if (e1.getX() - e2.getX() > 120) {

			// 是最后一页,滑动直接进入登陆界面
			if (this.flipper.getCurrentView().getId() == R.id.manual_last) {
				YoufangContext.saveManualShown();
				gotoMainActivity();
				this.finish();
				return false;
			}

			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));

			this.flipper.showNext();

			fling = true;
		} else if (e1.getX() - e2.getX() < -120) {

			// 第一页不能继续往前翻
			if (this.flipper.getCurrentView().getId() == R.id.manual_first) {
				return true;
			}

			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));

			this.flipper.showPrevious();
		}

		return fling;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {

		return false;
	}

}